#include <scheme.h>
#include <linux/version.h>
#include <linux/module.h>

#include <linux/security.h>
#include <linux/security.h>
#include <linux/uaccess.h>  
#include <linux/kallsyms.h>
#include "../../include/const_def.h"
#include "file_misc.h"
#include "config.h"
#include "globals.h"
#include "file_access.h"

/*
static char * __get_full_path(struct dentry *dentry, struct vfsmount *mnt)
{
	
	char * fullPath;
	struct path _path = {0};
	_path.mnt = mnt;
	_path.dentry = dentry;
	fullPath = ccs_realpath(&_path);
	return fullPath;
}
*/

extern int alg_file_digest (struct file *fp,unsigned char xh_digest[DIGEST_SIZE]);
int file_access(struct file * fp, const char * path)
{
	int retval = 0;
 
	long uid = (long)__xcurrent_uid();
	USER_ITEM * user_item = NULL;
	unsigned char xh_digest[DIGEST_SIZE];
	FILE_ITEM * file_item = NULL;
	
	if(!path) goto DONE;

	user_item = lookup_user_item((int)uid);
	if(!user_item) goto DONE;
	
  
	if(0 == strlen(g_globals.aclFolder)){
		goto DONE;
	}
	
	if(0 != strncmp(g_globals.aclFolder, path, strlen(g_globals.aclFolder))){
		goto DONE;
	}
		
	// -- goto DONE;
	
	if(0 != alg_file_digest(fp, xh_digest)){
		goto DONE;
	}
	
	file_item = lookup_file_item(xh_digest);
	if(!file_item) goto DONE;
	
	if(file_item->file.label.sec_label > user_item->user.label.sec_label){
		retval = -1;
		goto DONE;
	}
	
DONE:
	if(file_item){
		xobject_release(file_item);
	}
	
	if(user_item){
		xobject_release(user_item);
	}
 
	return retval;
}

